//
//  Status.swift
//  Weibo
//
//  Created by 冯童童 on 16/1/17.
//  Copyright © 2016年 冯童童. All rights reserved.
//

import UIKit

class Status: NSObject {
    var created_at : String?
    var id : Int64 = 0
    var text : String?
    var source : String?
    var user : User?
    var retweeted_status : Status?
    
    var pic_urls : [[String : String]]?{
        didSet {
            if let urls = pic_urls where urls.count != 0 {
                
                imageURLs = [NSURL]()
                for item in urls {
                    let urlString = item["thumbnail_pic"]
                    let imageURL = NSURL(string: urlString!)!
                    imageURLs?.append(imageURL)
                }
            }
        }
        
    }
    
    var imageURLs : [NSURL]?
    
    init(dict : [String : AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key ==  "user" {
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
                return 
            }
        }
        
        if key ==  "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
                
                return
            }
        }

        super.setValue(value, forKey: key)
       
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    override var description : String {
        let keys = ["created_at","id","text","source"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
}
